<?
require_once 'config.inc.php'; 
$uid = $_SESSION["uid"];
if (empty($_SESSION["username"])){
	header("Location:./admin/login.php");
}
if ($_GET['id']) {
	$id = intval($_GET['id']);
	$sql = "SELECT * FROM {$tablepre}book WHERE id=$id AND uid=$uid ";
	$book = $db->fetch_first($sql);
	
}else{
	$book['update_date'] = date('Y-m-d H:i:s');
}

$date_sql ='';
if($_GET['from']){
	$from = $_GET['from'];
}else{
	$from =  date('Y-m-d',strtotime('-1 month'));
}

if($_GET['to']){
	$to = $_GET['to'];
}else{
	$to =  date('Y-m-d',strtotime('-0 day'));
}

if($_GET['ccy']){
	$ccy = $_GET['ccy'];
}else{
	$ccy =  'CNY';
}


$date_sql .=" AND date>='$from'";
$date_sql .=" AND date<='$to'";
$date_sql .=" AND ccy='$ccy'";
//$date_sql ='';

$sql = "SELECT count(*) FROM {$tablepre}book WHERE  uid=$uid $date_sql ORDER BY update_date ASC,id ASC";
$intTotal = $db->result_first($sql);


$intShowNum = 30;
$sql = "SELECT count(*) FROM {$tablepre}book WHERE  uid=$uid $date_sql ORDER BY update_date ASC,id ASC";
$intTotal = $db->result_first($sql);		
$aPageDatas =   Pager ( $intTotal , $intShowNum);

$pagenav = show_pagenav($aPageDatas);
$limit ="LIMIT $aPageDatas[offset],$intShowNum";
			

$sql = "SELECT * FROM {$tablepre}book WHERE  uid=$uid $date_sql ORDER BY update_date ASC,id ASC $limit";
$books = $db->fetch_all($sql);




//总账
$sql = "SELECT otype,sum(amount) AS amount,count(id) AS num FROM {$tablepre}book WHERE  uid=$uid GROUP BY otype  ";
$counts = $db->fetch_all($sql);
foreach($counts as $k=>$v){
	$v['amount'] = sprintf('%1.2f',abs($v['amount']));
	$counts_total[$v['otype']] =$v;
}
$counts_total['TOTAL']['num'] = $counts_total['IN']['num']+$counts_total['OUT']['num'];
$counts_total['TOTAL']['amount'] = sprintf('%1.2f',$counts_total['IN']['amount']-$counts_total['OUT']['amount']);
//pecho($counts_total);


$sql = "SELECT otype,sum(amount) AS amount,count(id) AS num FROM {$tablepre}book WHERE  uid=$uid $date_sql GROUP BY otype  ORDER BY update_date ASC,id ASC  $limit";
$counts = $db->fetch_all($sql);
//pecho($counts);
foreach($counts as $k=>$v){
	$v['amount'] = sprintf('%1.2f',abs($v['amount']));
	$counts2[$v['otype']] =$v;
}
//pecho($counts2);
$_var['pagenav']=$pagenav;

$items=array('1'=>'银行','2'=>'支付宝','3'=>'现金','4'=>'信用卡');
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> 

<script type="text/javascript" src="js/class/facefiles/facebox.js"></script>
<link REL="StyleSheet" type="text/css" href="js/class/facefiles/facebox.css" />
<script>
jQuery(document).ready(function($) {
	$('a[rel*=facebox]').facebox();
});
</script>

<link rel="stylesheet" type="text/css" href="images/style.css">
<div class="note">
	<div class="title">帐本 <a href="/admin/admin_book.php" rel="facebox">添加帐目<img src="images/add.gif" width="16" height="16" border="0" style="vertical-align:middle" /></a></div>
	<div class="content">
	    <table cellspacing="0" cellpadding="0">
	        <tr>
	            <td> 账目总笔数： <span id="OutCount2">
	                <?=$counts_total['TOTAL']['num']?>
	                </span></td>
	            <td></td>
            </tr>
	        <tr>
	            <td>资金余额： <span id="OutTotalMoney2">
	                <?=$counts_total['TOTAL']['amount']?>
	                </span></td>
            </tr>
        </table>
	    <hr />
	    <form id="form1" name="form1" method="get" action="">
	    <label for="from"></label>
        起始日期：
	    <input name="from" type="text" id="from" size="10" value="<?=$from?>" />
        <a href="javascript:pubdate_cal[0].popup()"><img src="./js/class/calendar2/calendar.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date" /></a>
         终止日期：
	    <input name="to" type="text" id="to" size="10"  value="<?=$to?>"/>
        <a href="javascript:pubdate_cal[1].popup()"><img src="./js/class/calendar2/calendar.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date" /></a>
        
        <?php
		$_SYSTEM  = cores::get_system_config();
		$currency = json_decode($_SYSTEM['currency'],TRUE); 
		//$_SYSTEM['currency']=array('CNY'=>'人民币','USD'=>'美元');		
		//$currency = json_encode($_SYSTEM['currency']); 
		//$currency = json_decode($currency,TRUE);
		//pecho($currency);
		?>
	&nbsp;货币：
	<select name="ccy" id="ccy">
	    <option value="CNY" <?=set_select($ccy,'CNY')?>>CNY</option>
	    <option value="USD" <?=set_select($ccy,'USD')?>>USD</option>
	    </select>
	<input id="BtnOK" type="submit" value="查 询" name="BtnOK" />
	</form>
    

<script type="text/javascript" language="javascript" src="../js/class/calendar2/calendar.js"></script>
<script>
var pubdate_cal=[];
var i=0;

pubdate_cal[i] = new xar_base_calendar(document.getElementById("from"), "../"); 
pubdate_cal[i].year_scroll = true; 
pubdate_cal[i].time_comp = false;

i++;
pubdate_cal[i] = new xar_base_calendar(document.getElementById("to"), "../"); 
pubdate_cal[i].year_scroll = true; 
pubdate_cal[i].time_comp = false;


</script>
   
	<table cellspacing="1" cellpadding="5" border="0" class="table0 width_box">
	    <tr class="td0 c">
			        <th scope="col">日期</th>
			        <th scope="col">账户</th>
			        <th scope="col">用途</th>
			        <th scope="col">备注</th>
			        <th scope="col">货币</th>
			        <th scope="col">收入</th>
			        <th scope="col">支出</th>
			        <th scope="col">余额</th>
			        <th scope="col">操作</th>
	        </tr>
	<?php
	if($books){
		foreach($books as $k=>$v){
			?>        
              <?php $class == 'td2' ? $class  = 'td2alt':$class  = 'td2';?>
  				<tr class="<?=$class?> r">
			        <td><?=$v['update_date']?></td>
			        <td><?=$items[$v['item']]?></td>
			        <td><?=$v['item_txt']?></td>
			        <td><?=$v['remark']?></td>
			        <td><?=$v['ccy']?></td>
			        <td><?=$v['otype']=='IN'? $v['amount']:''?></td>
			        <td><?=$v['otype']!='IN'? $v['amount']:''?></td>
			        <td><?=$v['net']?></td>
			        <td><a href="/admin/admin_book.php?id=<?=$v['id']?>" rel="facebox">编辑</a>
                    <a href="/admin/admin_book.php?action=del&id=<?=$v['id']?>"  onclick="if(!confirm('确认删除'))return false">删除</a>	         
		            </td>
		        </tr>
<?php
		}
	}else{
		?>
              
			    <tr class="td2">
			        <td colspan="9">无帐目数据</td>
		        </tr>
         <?php
	}
	?>                 
	    </table>
	<br />

    <table cellspacing="0" cellpadding="0">
        <tr>
            <td> 支出交易笔数： <span id="OutCount"><?=$counts2['OUT']['num']?></span></td>
            <td>&nbsp; 收入交易笔数： <span id="InCount"><?=$counts2['IN']['num']?></span></td>
            <td></td>
        </tr>
        <tr>
            <td>支出金额合计： <span id="OutTotalMoney"><?=$counts2['OUT']['amount']?></span></td>
            <td>&nbsp; 收入金额合计： <span id="InTotalMoney"><?=$counts2['IN']['amount']?></span></td>
        </tr>
</table>
    <p>&nbsp;</p>
    <div class="pages"><?=$_var['pagenav']?></div>
    </div>
</div>
<script>

function edit(id){
	window.location.href='/view_book.php?id='+id;
	window.location.href='/admin/admin_book.php?id='+id;
	//document.getElementById('button').value='编辑';
	//document.getElementById('id').value='id';
	
}
</script>
<?
require_once 'foot.inc.php';
?>